﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class UpdateMessage : System.Web.UI.Page
{
    NewsDao nd = new NewsDao();
    NewsVo nv = new NewsVo();
    private static ArrayList ALBody = new ArrayList();
    EncriptManager em = new EncriptManager();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            UsersVo uv = new UsersVo();
            uv = (UsersVo)Session["user"];
            if (uv == null)
            {
                Response.Redirect("index.aspx");
            }
            ViewState["pageno"] = 0;
            string nid = em.Decript(Request.QueryString["nid"]);
            Session.Add("nid", nid);
            int page = 1;
            if (nid == "")
            {
                Response.Redirect("Main/MyMessage.aspx");
            }
            if (Request.QueryString["page"] != null)
            {
                page = Convert.ToInt32(Request.QueryString["page"].Trim());
            }
            nv = nd.getSingleNews(nid);
            bd(nv);
            TBBody.ImageGalleryPath = "images/users/" + uv.Uname + "/" + DateTime.Now.ToString("yyyyMMdd");
        }
    }

    void bd(NewsVo nv)
    {
        DataTable dt = new DataTable();
        dt = nd.getTopic();
        ddltopic.DataSource = dt;
        ddltopic.DataTextField = "Tname";
        ddltopic.DataValueField = "Tid";
        ddltopic.DataBind();
        ddltopic.SelectedValue = nv.Tid.ToString();
        dt = nd.getModule(ddltopic.SelectedValue);
        if (dt.Rows.Count > 0)
        {
            ddlmudule.DataSource = dt;
            ddlmudule.DataTextField = "Mname";
            ddlmudule.DataValueField = "Mid";
            ddlmudule.DataBind();
        }
        ddlmudule.SelectedValue = nv.Mid.ToString();
        TBTitle.Text = nv.Nmaintitle;
        string content = nv.Ncontent;
        ArrayList body = new ArrayList();
        ALBody.Clear();
        if (content.IndexOf("{$Page$}") > -1)
        {
            string[] temp = content.Split(new string[] { "{$Page$}" }, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < temp.Length; i++)
            {
                ALBody.Add(temp[i]);
            }
        }
        else
        {
            ALBody.Add(content);
        }
        TBBody.Text = (string)ALBody[0];
        AddDDL(ref DDLSelectPage, ALBody.Count, 1);
        SeletePageNow("1");
        IsButton();
    }
    private void AddDDL(ref DropDownList DDLTemp, int DDLCount, int selectIndex)
    {
        DDLTemp.Items.Clear();
        ListItem LITemp = null;
        for (int i = 1; i < DDLCount + 1; i++)
        {
            LITemp = new ListItem(i.ToString() + "页", i.ToString());
            if (i == selectIndex)
            {
                LITemp.Selected = true;
            }
            else
            {
                LITemp.Selected = false;
            }
            DDLTemp.Items.Add(LITemp);
        }
    }
    private void SeledeDLL(int selectIndex)
    {
        DDLSelectPage.ClearSelection();
        DDLSelectPage.Items[selectIndex].Selected = true;
    }
    private void InsertList(ref ArrayList Altemp, int sertIndex)
    {
        Altemp.Insert(sertIndex, "");
    }
    private void IsButton()
    {
        if (DDLSelectPage.Items.Count <= 1)
        {
            BLastPage.Enabled = false;
            BNextPage.Enabled = false;
            BDeletePage.Enabled = false;
        }
        else
        {
            if (DDLSelectPage.SelectedIndex < 0)
            {
                DDLSelectPage.SelectedIndex = 0;
            }
            BDeletePage.Enabled = true;
            if (DDLSelectPage.Items.Count < 1 || DDLSelectPage.SelectedIndex == 0)
            {
                BNextPage.Enabled = true;
                BLastPage.Enabled = false;
            }
            else if (DDLSelectPage.Items.Count == (DDLSelectPage.SelectedIndex + 1))
            {
                BLastPage.Enabled = true;
                BNextPage.Enabled = false;
            }
            else
            {
                BLastPage.Enabled = true;
                BNextPage.Enabled = true;
            }
        }
        SeletePageNow(DDLSelectPage.SelectedValue.ToString());
    }
    private void SeletePageNow(string pageNow)
    {
        LPageNow.Text = pageNow;
    }
    protected void BAddPage_Click(object sender, EventArgs e)
    {
        if (TBTitle.Text == "")
        {
            string script = "<script>window.alert('标题不能为空！');</script>";
            Page.RegisterClientScriptBlock("", script);
            return;
        }
        else if (TBBody.Text == "")
        {
            string script = "<script>window.alert('内容不能为空！');</script>";
            Page.RegisterClientScriptBlock("", script);
            return;
        }
        else
        {
            if (DDLSelectPage.SelectedIndex < 0)
            {
                DDLSelectPage.SelectedIndex = 0;
            }
            if ((DDLSelectPage.SelectedIndex + 1) == DDLSelectPage.Items.Count)
            {
                ALBody[DDLSelectPage.SelectedIndex] = TBBody.Text;
                TBBody.Text = "";
                InsertList(ref ALBody, DDLSelectPage.SelectedIndex + 1);
                AddDDL(ref DDLSelectPage, ALBody.Count, DDLSelectPage.SelectedIndex + 2);
                SeletePageNow((DDLSelectPage.SelectedIndex + 1).ToString());
            }
            else
            {
                int selectIndex = DDLSelectPage.SelectedIndex + 1;
                InsertList(ref ALBody, selectIndex);
                AddDDL(ref DDLSelectPage, DDLSelectPage.Items.Count + 1, DDLSelectPage.SelectedIndex + 2);
                TBBody.Text = "";
            }
        }
        IsButton();
    }
    protected void DDLSelectPage_SelectedIndexChanged(object sender, EventArgs e)
    {
        IsButton();
        if (DDLSelectPage.SelectedIndex < 0)
        {
            DDLSelectPage.SelectedIndex = 0;
        }
        TBBody.Text = (string)ALBody[DDLSelectPage.SelectedIndex];
    }
    protected void BLastPage_Click(object sender, EventArgs e)
    {
        if (BLastPage.Enabled)
        {
            if (DDLSelectPage.SelectedIndex < 0)
            {
                DDLSelectPage.SelectedIndex = 0;
            }
            TBBody.Text = (string)ALBody[DDLSelectPage.SelectedIndex - 1];
            SeledeDLL(DDLSelectPage.SelectedIndex - 1);
        }
        IsButton();
    }
    protected void BNextPage_Click(object sender, EventArgs e)
    {
        if (BNextPage.Enabled)
        {
            if (DDLSelectPage.SelectedIndex < 0)
            {
                DDLSelectPage.SelectedIndex = 0;
            }
            TBBody.Text = (string)ALBody[DDLSelectPage.SelectedIndex + 1];
            SeledeDLL(DDLSelectPage.SelectedIndex + 1);
        }
        IsButton();
    }
    protected void TBBody_TextChanged(object sender, EventArgs e)
    {
        if (DDLSelectPage.SelectedIndex < 0)
        {
            DDLSelectPage.SelectedIndex = 0;
        }
        ALBody[DDLSelectPage.SelectedIndex] = TBBody.Text;
    }
    protected void BDeletePage_Click(object sender, EventArgs e)
    {
        if (BDeletePage.Enabled)
        {
            if (DDLSelectPage.SelectedIndex < 0)
            {
                DDLSelectPage.SelectedIndex = 0;
            }
            ALBody.RemoveAt(DDLSelectPage.SelectedIndex);
            AddDDL(ref DDLSelectPage, ALBody.Count, ALBody.Count);
            TBBody.Text = (string)ALBody[ALBody.Count - 1];
            IsButton();
        }
    }
    protected void BAddSubmit_Click(object sender, EventArgs e)
    {
        if (ddltopic.SelectedValue == "0")
        {
            string script = "<script>window.alert('请选择板块！');</script>";
            Page.RegisterClientScriptBlock("", script);
            return;
        }
        if (ddltopic.SelectedValue == "1")
        {
            if (ddlmudule.SelectedValue == "--请选择--")
            {
                string script = "<script>window.alert('请选择类型！');</script>";
                Page.RegisterClientScriptBlock("", script);
                return;
            }
        }
        if (this.TBTitle.Text == "")
        {
            string script = "<script>window.alert('标题不能为空！');</script>";
            Page.RegisterClientScriptBlock("", script);
            return;
        }
        else if (TBBody.Text == "")
        {
            string script = "<script>window.alert('内容不能为空！');</script>";
            Page.RegisterClientScriptBlock("", script);
            return;
        }
        else
        {
            if (DDLSelectPage.SelectedIndex < 0)
            {
                DDLSelectPage.SelectedIndex = 0;
            }
            ALBody[DDLSelectPage.SelectedIndex] = TBBody.Text;
            string title = TBTitle.Text.Trim();
            string body = "";
            if (ALBody.Count < 2)
            {
                body = (string)ALBody[0];
            }
            else
            {
                for (int i = 0; i < ALBody.Count; i++)
                {
                    if (i == 0)
                    {
                        body = body + (string)ALBody[i];
                    }
                    else
                    {
                        body = body + "{$Page$}" + (string)ALBody[i];
                    }
                }
            }
            Exception ex = addnews(body);
            if (ex == null)
            {
                ALBody.Clear();
                body = "";
            }
        }
    }

    Exception addnews(string body)
    {
        UsersVo uv = new UsersVo();
        uv = (UsersVo)Session["user"];
        NewsVo nv = new NewsVo();
        nv.Nid = Convert.ToInt32(Session["nid"]);
        nv.Nmaintitle = TBTitle.Text;
        nv.Nsubhead = "";
        nv.Ncontent = body.Replace("'", " ");
        nv.Tid = Convert.ToInt32(ddltopic.SelectedValue);
        if (ddlmudule.SelectedValue == "--请选择--")
        {
            nv.Mid = 0;
        }
        else
        {
            nv.Mid = Convert.ToInt32(ddlmudule.SelectedValue);
        }
        nv.Ntime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        nv.Nauthor = uv.Unickname;
        nv.Nposter = HiddenField1.Value;
        nv.Nstate = 1;
        nv.Uid = uv.Uid;
        Exception ex = new Exception();
        ex = nd.updateSingleNews(nv);
        if (ex == null)
        {
            string script = "<script>window.alert('修改成功！');location.href='Main/MyMessage.aspx';</script>";
            Page.RegisterClientScriptBlock("", script);
        }
        else
        {
            Response.Write(ex.ToString());
        }
        return ex;
    }

    protected void ddtopic_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlmudule.Items.Clear();
        ddlmudule.Items.Add("--请选择--");
        DataTable dt = nd.getModule(ddltopic.SelectedValue);
        if (dt.Rows.Count > 0)
        {
            ddlmudule.DataSource = dt;
            ddlmudule.DataTextField = "Mname";
            ddlmudule.DataValueField = "Mid";
            ddlmudule.DataBind();
        }
    }

    protected void btncancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("index.aspx");
    }
}
